package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.AnswerVo;
import org.dromara.business.domain.bo.AnswerBo;
import org.dromara.business.service.AnswerService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 客服问答
 *
 * @author zlj
 * @date 2024-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/answer")
public class AnswerController extends BaseController {

    private final AnswerService answerService;

    /**
     * 查询客服问答列表
     */
    @GetMapping("/list")
    public TableDataInfo<AnswerVo> list(AnswerBo bo, PageQuery pageQuery) {
        return answerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客服问答列表
     */
    @SaCheckPermission("business:answer:export")
    @Log(title = "客服问答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AnswerBo bo, HttpServletResponse response) {
        List<AnswerVo> list = answerService.queryList(bo);
        ExcelUtil.exportExcel(list, "客服问答", AnswerVo.class, response);
    }

    /**
     * 获取客服问答详细信息
     *
     * @param answerId 主键
     */
    @SaCheckPermission("business:answer:query")
    @GetMapping("/{answerId}")
    public R<AnswerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long answerId) {
        return R.ok(answerService.queryById(answerId));
    }

    /**
     * 新增客服问答
     */
    @SaCheckPermission("business:answer:add")
    @Log(title = "客服问答", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AnswerBo bo) {
        return toAjax(answerService.insertByBo(bo));
    }

    /**
     * 修改客服问答
     */
    @SaCheckPermission("business:answer:edit")
    @Log(title = "客服问答", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AnswerBo bo) {
        return toAjax(answerService.updateByBo(bo));
    }

    /**
     * 删除客服问答
     *
     * @param answerIds 主键串
     */
    @SaCheckPermission("business:answer:remove")
    @Log(title = "客服问答", businessType = BusinessType.DELETE)
    @DeleteMapping("/{answerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] answerIds) {
        return toAjax(answerService.deleteWithValidByIds(List.of(answerIds), true));
    }
}
